<template>
	<view>
		<view class="detail-wrap ">

			<view class="detail-row">
				<view class="detail-row-label">
					<text>来源单号</text>
				</view>
				<view class="detail-row-value">
					<input class="wc-input" type="text" disabled :value="mainOrder.FRBILLNO" placeholder="" />
				</view>
			</view>
			<view class="detail-row">
				<view class="detail-row-label">
					<text>集团</text>
				</view>
				<view class="detail-row-value">
					<input class="wc-input" type="text" disabled :value="mainOrder.GROUPNAME" placeholder="" />
				</view>
			</view>
			<view class="detail-row" @tap="activeIndexM(2,'BA_ORGANIZE')">
				<view class="detail-row-label">
					<text>组织</text>
				</view>
				<view class="detail-row-value">
					<input class="wc-input " name="STORENAME" type="text" disabled :value="mainOrder.ORGNAME" placeholder="" />
				</view>
			</view>

			<view class="detail-row" @tap="activeIndexM(0,'BA_DEPARTMENT')">
				<view class="detail-row-label">
					<text>部门</text>
				</view>
				<view class="detail-row-value">
					<input class="wc-input " disabled name="STORENAME" type="text" :value="mainOrder.DEPTNAME" placeholder="" />
					<!-- <input type="number" class="text-black" :disabled="orderType==1" v-model="mainTable.APPLYERTEL" placeholder="请输入领用人电话" /> -->
				</view>
			</view>

			<!-- <view class="detail-row" @click="dateEvent('APPLYERDATE')">
				<view class="detail-row-label">
					<text>单据日志</text>
				</view>
				<view class="detail-row-value" :class="orderType==0?'text-blue' :'' ">
					{{ mainTable.APPLYERDATE }}
				</view>
			</view> -->

			<view class="detail-row">
				<view class="detail-row-label">
					<text>备注</text>
				</view>
				<view class="detail-row-value">
					<input type="text" class="text-black" v-model="mainOrder.VMEMO" placeholder="请输入备注" />
				</view>
			</view>
		</view>

		<view class=" detail-sub-wrap" v-for="(item,index) in materList">
			<view class="detail-row">
				<view class="detail-row-label">
					<text>物资单号</text>
				</view>
				<view class="detail-row-value text-blue">
					{{ item.BILLNO || item.PK_MATERIAL }}
				</view>
			</view>
			<view class="detail-row">
				<view class="detail-row-label">
					<text>物资名称</text>
				</view>
				<view class="detail-row-value">
					{{ item.MATERIALNAME  }}
				</view>
			</view>
			<view class="detail-row">
				<view class="detail-row-label">
					<text>规格</text>
				</view>
				<view class="detail-row-value">
					{{ item.MATSPEC  }}
				</view>
			</view>
			<view class="detail-row">
				<view class="detail-row-label">
					<text>货位</text>
				</view>
				<view class="detail-row-value">
					{{ item.STATIONNAME  }}
				</view>
			</view>
			<view class="detail-row">
				<view class="detail-row-label">
					<text>出库数量</text>
				</view>
				<view class="detail-row-value">
					{{ item.NNUMSQ || '0' }}({{ item.UNIT || '' }})
				</view>
			</view>
			<view class="detail-row">
				<view class="detail-row-label">
					<text>红冲数量</text>
				</view>
				<view class="detail-row-value">
					<input type="number"  class="text-black" v-model="item.NNUM" placeholder="请输入数量" />
				</view>
			</view>
		</view>

		<view class="detail-wrap-btn">
			<button class="cu-btn block bg-blue margin-tb-sm lg" @click="submit">
				提交
			</button>
		</view>

		<w-picker mode="date" startYear="2000" endYear="2050" step="1" :defaultVal="[1,1,1,1,2,5]" :current="true" @confirm="onConfirm"
		 ref="dateTime" themeColor="#f00"></w-picker>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import wPicker from "@/components/w-picker/w-picker.vue";
	import api from '@/common/api.js'
	let _self, dateTimekey, activeType;
	let query = {
		"category": "ST_OUTSTOCK",
		"page": 1,
		"rows": "50",
		"sidx": "",
		"sord": "",
		"queryJson": {},
		"userId": "",
		"account": "admin"
	}

	export default {
		computed: mapState(['userInfo', 'checkData']),
		components: {
			wPicker
		},
		data() {
			return {
				orderType: 0, // 0 是借出  1 是归还
				mainOrder: {
					"FRBILLNO": "",
					"PK_GROUP": "",
					"GROUPNAME": "",
					"PK_ORG": "",
					"ORGNAME": "",
					"PK_DEPT": "",
					"DEPTNAME": "",
					"PK_APPLYER": "",
					"APPLYER": "",
					"PK_SUPPLY": "",
					"SUPPLYNAME": "",
					"PK_STORE": "",
					"STORENAME": "",
					"BTYPE": 1,
					"VMEMO": "",
					"userId": "",
					"account": ""
				},
				mainTable: {
					BILLNO: '',
					FRBILLNO: '',
					APPLYER: '',
					APPLYERTEL: '',
					BACKBY: '',
					BACKDATE: '',
					VMEMO: ''
				},
				materList: []
			};
		},
		onLoad(p) {
			_self = this
			let abc = JSON.parse(decodeURIComponent(p.item));
			this.initSetData(abc);
			// console.log(abc)
			// _self.planList = abc
		},
		
		onShow(){
			
		},
		
		methods: {
			initSetData(data) {
				
				query.userId = _self.userInfo.UserId
				query.account = _self.userInfo.Account
				_self.mainOrder.FRBILLNO = data.BILLNO
				_self.mainOrder.GROUPNAME = _self.userInfo.GROUPNAME || ''
				_self.mainOrder.PK_GROUP = _self.userInfo.PK_GROUP
				_self.mainOrder.ORGNAME = _self.userInfo.ORGNAME
				_self.mainOrder.PK_ORG = _self.userInfo.ORGANIZEID
				_self.mainOrder.DEPTNAME = data.DEPTNAME || _self.userInfo.DEPTNAME
				_self.mainOrder.PK_DEPT = data.PK_DEPT || _self.userInfo.DEPARTMENTID
				_self.mainOrder.APPLYER = _self.userInfo.Account
				_self.mainOrder.PK_APPLYER = _self.userInfo.UserId
				_self.mainOrder.userId = _self.userInfo.UserId
				_self.mainOrder.account = _self.userInfo.Account
				
				this.getDetail()
			},
			
			activeIndexM(i, t) {
				_self.activeLineIndex = i;
				activeType = t;
				let queryParames = '',
					pk_my_org = '';
				if (t == 'BA_DEPARTMENT') {
					pk_my_org = this.mainOrder.PK_ORG
				} else if (t == 'BA_ORGANIZE') {
					pk_my_org = this.mainOrder.PK_GROUP
				}
				uni.navigateTo({
					url: '/pages/public/search?t=' + t + '&organizeId=' + pk_my_org + '&queryjson=' + queryParames
				})
			},
			
			submit() {
				let URL = api.apiPublic.SaveOutStore;
				let savedata = {
					..._self.mainOrder,
					"Detail": _self.materList
				}
				console.log(URL);
				console.log(JSON.stringify(savedata));
				_self.axios({
						url: URL,
						data: savedata,
						isLoading: true
					})
					.then(res => {
						let {
							IsSuccess,
							Message
						} = res;

						_self.$utils.tips('提交成功')

						setTimeout(() => {
							uni.$emit('refresh', {
								isRefresh: true
							})
							uni.navigateBack({})
						}, 1600)
					})
			},
			onConfirm(val) {
				this.mainTable[dateTimekey] = val.result;
			},

			getDetail() {
				let URL =  api.apiPublic.getMaterList;
				query.queryJson = JSON.stringify({PK_OUTSTOCK:_self.mainOrder.FRBILLNO})
				// console.log(URL);
				// console.log(query);
				_self.axios({
						url: URL,
						data: query,
						isLoading: true
					})
					.then(res => {
						let {
							IsSuccess,
							Message,
							Result
						} = res;
						let Arrays = Result.rows.map(item=>{
							item.NNUM = '' 
							return item;
						})
						_self.materList = Arrays
					})
			},

			dateEvent(id) {
				if (this.orderType == 1 && id == 'APPLYERDATE') return;
				dateTimekey = id;
				this.$refs.dateTime.show();
			},
		}
	}
</script>

<style lang="scss">
	.text-blue {
		color: #1677ff;
	}
	.detail-row-value{
		input{
			font-size: 35rpx !important;
		}
	}
</style>
